﻿using ACGN.Application.NewsFlashType;
using ACGN.Application.NewsFlashType.Dtos;
using ACGN.Core.Models;
using ACGN.Core.Repository;
using ACGN.Mvc.Controllers;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ACGN.MVC.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class NewsFlashTypeController : BaseController
    {
        private readonly ILogger<NewsFlashController> _logger;
        private readonly IRepository<NfType, int> _repository;
        private readonly INewsFlashTypeService _newsflashService;
        private readonly IMapper _mapper;

        public NewsFlashTypeController(ILogger<NewsFlashController> logger,
            IRepository<NfType, int> repository,
            INewsFlashTypeService newsflashService,
            IMapper mapper
            )
        {
            _logger = logger;
            _repository = repository;
            _newsflashService = newsflashService;
            _mapper = mapper;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<IActionResult> List(NewsFlashTypeInputDto input)
        {
            var newsflashtype = await _newsflashService.GetPageResult(input);
            return Json(ListResult(_mapper.Map<List<NfType>>(newsflashtype.Data), newsflashtype.TotalCount));
        }
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult<NfType>> AddNewsType(NfType nfType)
        {
            await _repository.InsertAsync(nfType);
            return CreatedAtAction(nameof(List), new { id = nfType.NFTId }, nfType);
        }
        public async Task<IActionResult> Edit(int nftId)
        {
            NfType nfType = await _repository.FirstOrDefaultAsync(s => s.NFTId == nftId);
            return View(_mapper.Map<NfType>(nfType));
        }
        [HttpPost]
        public async Task<ActionResult> EditNewsType(int nftId, NfType nfType)
        {
            if (nftId != nfType.NFTId)
            {
                return Json(AjaxResult(false));
            }
            await _repository.UpdateAsync(nfType);
            return Json(AjaxResult(true));
        }
        public async Task<ActionResult> Delete(int NFTId, NfType nfType)
        {
            if (NFTId != nfType.NFTId)
            {
                return Json(AjaxResult(false));
            }
            await _repository.DeleteAsync(nfType);
            return Json(AjaxResult(true));
        }
    }
}
